package com.epalmpay.service.groupaccount.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.epalmpay.exception.ApplicationException;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.util.AmountUtils;
import org.springframework.stereotype.Service;

import com.epalmpay.dto.datareport.GroupAccountInDTO;
import com.epalmpay.dto.datareport.GroupAccountLogDayOutDTO;
import com.epalmpay.dto.datareport.GroupAccountLogDaySumOutDTO;
import com.epalmpay.dto.datareport.GroupAccountLogMonthOutDTO;
import com.epalmpay.dto.datareport.GroupAccountLogMonthSumOutDTO;
import com.epalmpay.dto.datareport.GroupAccountLogOutDTO;
import com.epalmpay.dto.datareport.GroupAccountOutDTO;
import com.epalmpay.entity.GroupAccount;
import com.epalmpay.entity.GroupAccountLog;
import com.epalmpay.entity.GroupAccountLogDay;
import com.epalmpay.entity.GroupAccountLogDaySum;
import com.epalmpay.entity.GroupAccountLogMonth;
import com.epalmpay.entity.GroupAccountLogMonthSum;
import com.epalmpay.entity.Page;
import com.epalmpay.mapper.GroupAccountLogDayMapper;
import com.epalmpay.mapper.GroupAccountLogDaySumMapper;
import com.epalmpay.mapper.GroupAccountLogMapper;
import com.epalmpay.mapper.GroupAccountLogMonthMapper;
import com.epalmpay.mapper.GroupAccountLogMonthSumMapper;
import com.epalmpay.mapper.GroupAccountMapper;
import com.epalmpay.service.groupaccount.IGroupAccountService;

@Service("groupAccountService")
public class GroupAccountServiceImpl implements IGroupAccountService{
	
	@Resource
	private GroupAccountMapper groupAccountMapper;
	
	@Resource
	private GroupAccountLogDaySumMapper groupAccountLogDaySumMapper;
	
	@Resource
	private GroupAccountLogMonthSumMapper groupAccountLogMonthSumMapper;
	
	@Resource
	private GroupAccountLogDayMapper groupAccountLogDayMapper;
	
	@Resource
	private GroupAccountLogMonthMapper groupAccountLogMonthMapper;
	
	@Resource
	private GroupAccountLogMapper groupAccountLogMapper;
	
	
	@Resource
	private IGroupAccountService groupAccountService;

	@Override
	public List<GroupAccountLogDaySumOutDTO> getGroupAccountDayList(GroupAccountInDTO groupAccountInDTO, Page page) {
		
			Map<String,Object> param=new HashMap<String,Object>();
//			param.put("accountId",groupAccountInDTO.getGroupId());
//			param.put("joinStart", groupAccountInDTO.getJoinStart());
//			param.put("joinEnd", groupAccountInDTO.getJoinEnd());
			param.put("groupId", groupAccountInDTO.getGroupId());
			
//			
			if (groupAccountInDTO.getStringStatisDate()==null) {
				param.put("joinStart", groupAccountInDTO.getJoinStart());
				param.put("joinEnd", groupAccountInDTO.getJoinEnd());
			} else {
				param.put("joinStart", groupAccountInDTO.getStringStatisDate());
				param.put("joinEnd", groupAccountInDTO.getStringStatisDateEnd());
			}
			
			List<GroupAccountLogDaySumOutDTO> result = new ArrayList<GroupAccountLogDaySumOutDTO>();

			int sumCount = groupAccountLogDaySumMapper.selectGroupAccountLogDaySumListCount(param);
			page.setTotalResult(sumCount);
			if(sumCount<=0)
				return null;
			
			param.put("limit", page.getShowCount());
			param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
			
			List<Map<String,Object>>  groupAccountLogDaySumList = groupAccountLogDaySumMapper.selectGroupAccountLogDaySumList(param);
			
			
			for (Map<String, Object> groupAccountLogDaySum : groupAccountLogDaySumList) {
				
				GroupAccountLogDaySumOutDTO groupAccountDaySumOutDTO = new GroupAccountLogDaySumOutDTO();
				
				groupAccountDaySumOutDTO.setId((Long)(groupAccountLogDaySum.get("id")));
//				groupAccountDaySumOutDTO.setDaySumId((Long)(groupAccountLogDaySum.get("id")));
				groupAccountDaySumOutDTO.setAddAmount((Integer)groupAccountLogDaySum.get("add_amount"));
				groupAccountDaySumOutDTO.setUseAmount((Integer)groupAccountLogDaySum.get("use_amount"));
				groupAccountDaySumOutDTO.setFreezeAmount((Integer)groupAccountLogDaySum.get("freeze_amount"));
				groupAccountDaySumOutDTO.setUnfreezeAmount((Integer)groupAccountLogDaySum.get("unfreeze_amount"));
				groupAccountDaySumOutDTO.setStatisDate((Date)groupAccountLogDaySum.get("statis_date"));
				
				result.add(groupAccountDaySumOutDTO);
			}
			
			return result;
	}

	@Override
	public List<GroupAccountLogMonthSumOutDTO> getGroupAccountMonthList(GroupAccountInDTO groupAccountInDTO, Page page) {
		
		Map<String,Object> param=new HashMap<String,Object>();
//		param.put("accountId",groupAccountInDTO.getGroupId());
		
		param.put("groupId", groupAccountInDTO.getGroupId());
		
		if (groupAccountInDTO.getStringStatisDate()==null) {
			param.put("joinStart", groupAccountInDTO.getJoinStart());
			param.put("joinEnd", groupAccountInDTO.getJoinEnd());
		} else {
			param.put("joinStart", groupAccountInDTO.getStringStatisDate());
			param.put("joinEnd", groupAccountInDTO.getStringStatisDateEnd());
		}
		
		List<GroupAccountLogMonthSumOutDTO> result = new ArrayList<GroupAccountLogMonthSumOutDTO>();

		int sumCount = groupAccountLogMonthSumMapper.selectGroupAccountLogMonthSumListCount(param);
		page.setTotalResult(sumCount);
		if(sumCount<=0)
			return null;
		
		param.put("limit", page.getShowCount());
		param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
		
		List<Map<String,Object>>  groupAccountLogMonthSumList = groupAccountLogMonthSumMapper.selectGroupAccountLogMonthSumList(param);
		
		
		for (Map<String, Object> groupAccountLogMonthSum : groupAccountLogMonthSumList) {
			GroupAccountLogMonthSumOutDTO groupAccountLogMonthSumOutDTO = new GroupAccountLogMonthSumOutDTO();
			
			groupAccountLogMonthSumOutDTO.setId((Long)groupAccountLogMonthSum.get("id"));
			groupAccountLogMonthSumOutDTO.setAddAmount((Integer)groupAccountLogMonthSum.get("add_amount"));
			groupAccountLogMonthSumOutDTO.setUseAmount((Integer)groupAccountLogMonthSum.get("use_amount"));
			groupAccountLogMonthSumOutDTO.setFreezeAmount((Integer)groupAccountLogMonthSum.get("freeze_amount"));
			groupAccountLogMonthSumOutDTO.setUnfreezeAmount((Integer)groupAccountLogMonthSum.get("unfreeze_amount"));
			groupAccountLogMonthSumOutDTO.setStatisDate((String)groupAccountLogMonthSum.get("statis_date"));
			
			result.add(groupAccountLogMonthSumOutDTO);
		}
		
		return result;
	}

	@Override
	public GroupAccount getAmount(GroupAccountInDTO groupAccountInDTO) {
		return groupAccountMapper.getAmount(groupAccountInDTO.getGroupId());
	}

	@Override
	public GroupAccountLogDaySum queryDaySumStatisDate(GroupAccountLogDaySum groupAccountLogDaySum) {
		return groupAccountLogDaySumMapper.queryDaySumStatisDate(groupAccountLogDaySum.getId());
	}

	@Override
	public GroupAccountLogMonthSum queryMonthSumStatisDate(GroupAccountLogMonthSum groupAccountLogMonthSum) {
		return groupAccountLogMonthSumMapper.queryMonthSumStatisDate(groupAccountLogMonthSum.getId());
	}
	
	//查看详情
	@Override
	public List<GroupAccountLogDayOutDTO> getGroupAccountLogDayList(GroupAccountInDTO groupAccountInDTO, Page page) {
		Map<String,Object> param=new HashMap<String,Object>();
//		param.put("accountId",groupAccountInDTO.getGroupId());
		param.put("accountId", groupAccountInDTO.getAccountId());
		param.put("changeType", groupAccountInDTO.getChangeType());//用于条件搜索
		param.put("statisDate", groupAccountInDTO.getStatisDate());
		
		
		
		List<GroupAccountLogDayOutDTO> result = new ArrayList<GroupAccountLogDayOutDTO>();

		int sumCount = groupAccountLogDayMapper.selectGroupAccountLogDayListCount(param);
		page.setTotalResult(sumCount);
		if(sumCount<=0)
			return null;
		
		param.put("limit", page.getShowCount());
		param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
		
		List<Map<String,Object>>  groupAccountLogDayList = groupAccountLogDayMapper.selectGroupAccountLogDayList(param);
		
		
		for (Map<String, Object> groupAccountLogDay : groupAccountLogDayList) {
			GroupAccountLogDayOutDTO groupAccountLogDayOutDTO = new GroupAccountLogDayOutDTO();
			
			groupAccountLogDayOutDTO.setId((Long)groupAccountLogDay.get("id"));
			groupAccountLogDayOutDTO.setStatisDate((Date)groupAccountLogDay.get("statis_date"));
			groupAccountLogDayOutDTO.setAmountSum(Integer.parseInt(groupAccountLogDay.get("amount_sum").toString()));
			groupAccountLogDayOutDTO.setChangeType((Integer)groupAccountLogDay.get("change_type"));
			groupAccountLogDayOutDTO.setSubjectType((Integer)groupAccountLogDay.get("subject_type"));
			
			result.add(groupAccountLogDayOutDTO);
		}
		
		return result;
	}

	@Override
	public List<GroupAccountLogMonthOutDTO> getGroupAccountLogMonthList(GroupAccountInDTO groupAccountInDTO,
			Page page) {
		Map<String,Object> param=new HashMap<String,Object>();
		
//		param.put("accountId",groupAccountInDTO.getGroupId());
		param.put("accountId", groupAccountInDTO.getAccountId());
		
		param.put("statisDate", groupAccountInDTO.getStringStatisDate());
		
		param.put("changeType", groupAccountInDTO.getChangeType());
		
		
		List<GroupAccountLogMonthOutDTO> result = new ArrayList<GroupAccountLogMonthOutDTO>();

		int sumCount = groupAccountLogMonthMapper.selectGroupAccountLogMonthListCount(param);
		page.setTotalResult(sumCount);
		if(sumCount<=0)
			return null;
		
		param.put("limit", page.getShowCount());
		param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
		
		List<Map<String,Object>>  groupAccountLogDayList = groupAccountLogMonthMapper.selectGroupAccountLogMonthList(param);
		
		
		for (Map<String, Object> groupAccountLogDay : groupAccountLogDayList) {
			GroupAccountLogMonthOutDTO groupAccountLogMonthOutDTO = new GroupAccountLogMonthOutDTO();
			
			groupAccountLogMonthOutDTO.setId((Long)groupAccountLogDay.get("id"));
			groupAccountLogMonthOutDTO.setStatisDate((String)groupAccountLogDay.get("statis_date"));
			groupAccountLogMonthOutDTO.setAmountSum(Integer.parseInt(groupAccountLogDay.get("amount_sum").toString()));
			groupAccountLogMonthOutDTO.setChangeType((Integer)groupAccountLogDay.get("change_type"));
			groupAccountLogMonthOutDTO.setSubjectType((Integer)groupAccountLogDay.get("subject_type"));
			
			result.add(groupAccountLogMonthOutDTO);
		}
		
		return result;
	}

	@Override
	public GroupAccount queryData(GroupAccount groupAccount) {
		return groupAccountMapper.queryData(groupAccount.getGroupId());
	}
	
	
	
	@Override
	public GroupAccountLogDay queryAccountById(Long id) {
		return groupAccountLogDayMapper.queryAccountById(id);
	}

	
	@Override
	public List<GroupAccountLogOutDTO> getGroupAccountLogList(GroupAccountInDTO groupAccountInDTO, Page page) {
		
		Map<String,Object> param=new HashMap<String,Object>();
		
		
		param.put("accountId",groupAccountInDTO.getAccountId());
		param.put("subjectType", groupAccountInDTO.getSubjectType());
		param.put("statisDate", groupAccountInDTO.getStatisDate());

		param.put("changeType", groupAccountInDTO.getChangeType());

		
		
		List<GroupAccountLogOutDTO> result = new ArrayList<GroupAccountLogOutDTO>();

		int sumCount = groupAccountLogMapper.selectGroupAccountLogListCount(param);
		
		page.setTotalResult(sumCount);
		if(sumCount<=0)
			return null;
		
		param.put("limit", page.getShowCount());
		param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
		
		List<Map<String,Object>>  groupAccountLogList = groupAccountLogMapper.selectGroupAccountLogList(param);
		
		
		for (Map<String, Object> groupAccountLogDay : groupAccountLogList) {
			GroupAccountLogOutDTO groupAccountLogOutDTO = new GroupAccountLogOutDTO();
//			groupAccountLogOutDTO.setId((Long)groupAccountLogDay.get("id"));
			groupAccountLogOutDTO.setAmount((Integer)groupAccountLogDay.get("amount"));
			groupAccountLogOutDTO.setName((String)groupAccountLogDay.get("name"));
			groupAccountLogOutDTO.setSubjectType((Integer)groupAccountLogDay.get("subject_type"));
			groupAccountLogOutDTO.setDescInfo((String)groupAccountLogDay.get("desc_info"));
			groupAccountLogOutDTO.setGmtCreate((Date)groupAccountLogDay.get("gmt_create"));
			groupAccountLogOutDTO.setGetTime((Date)groupAccountLogDay.get("get_time"));
			if ((Integer)groupAccountLogDay.get("subject_type")==6 ||(Integer)groupAccountLogDay.get("subject_type")==7 ) {
				groupAccountLogOutDTO.setGetStatus((Integer)groupAccountLogDay.get("get_status"));
				if(groupAccountLogDay.get("createName")!=null){
					groupAccountLogOutDTO.setCreateName(groupAccountLogDay.get("createName").toString());
				}
			}
			if ((Integer)groupAccountLogDay.get("subject_type")==5) {
				groupAccountLogOutDTO.setGetStatus(Integer.parseInt(String.valueOf(groupAccountLogDay.get("status"))));
				if(groupAccountLogDay.get("createName")!=null){
					groupAccountLogOutDTO.setCreateName(groupAccountLogDay.get("createName").toString());
				}

			}
			if ((Integer)groupAccountLogDay.get("subject_type")==11 ||(Integer)groupAccountLogDay.get("subject_type")==12 ) {
				groupAccountLogOutDTO.setCouponUseStatus((Integer)groupAccountLogDay.get("coupon_use_status"));
				groupAccountLogOutDTO.setCouponCode((String)groupAccountLogDay.get("coupon_code"));
				if(groupAccountLogDay.get("createName")!=null){
					groupAccountLogOutDTO.setCreateName(groupAccountLogDay.get("createName").toString());
				}
			}
			if ((Integer)groupAccountLogDay.get("subject_type")==10) {
				groupAccountLogOutDTO.setCouponUseStatus(Integer.parseInt(String.valueOf(groupAccountLogDay.get("status"))));
				groupAccountLogOutDTO.setCouponCode((String)groupAccountLogDay.get("coupon_code"));
				if(groupAccountLogDay.get("createName")!=null){
					groupAccountLogOutDTO.setCreateName(groupAccountLogDay.get("createName").toString());
				}
			}
			if ((Integer)groupAccountLogDay.get("subject_type")==15) {
				groupAccountLogOutDTO.setGetStatus(Integer.parseInt(String.valueOf(groupAccountLogDay.get("status"))));
				if(groupAccountLogDay.get("receiver_name")!=null) {
					groupAccountLogOutDTO.setCreateName(groupAccountLogDay.get("receiver_name").toString());
				}
			}
			if ((Integer)groupAccountLogDay.get("subject_type")==17) {
				if(groupAccountLogDay.get("receiver_name")!=null) {
					groupAccountLogOutDTO.setCreateName(groupAccountLogDay.get("receiver_name").toString());
				}
			}

			result.add(groupAccountLogOutDTO);
		}
		
		return result;
	}

	

//	@Override
//	public GroupAccountListOutDTO getGroupAccountLogList(GroupAccountInDTO groupAccountInDTO, Page page) {
//		GroupAccountListOutDTO groupAccountListOutDTO = new GroupAccountListOutDTO();
//		
//		Map<String,Object> param=new HashMap<String,Object>();
//		
//		
//		param.put("accountId",groupAccountInDTO.getAccountId());
//		
//		param.put("subjectType", groupAccountInDTO.getSubjectType());
//		
//		param.put("statisDate", groupAccountInDTO.getStatisDate());
//		
//		List<GroupAccountLogOutDTO> result = new ArrayList<GroupAccountLogOutDTO>();
//		
//		int sumCount = groupAccountLogMapper.selectGroupAccountLogListCount(param);
//		
//		page.setTotalResult(sumCount);
//		if(sumCount<=0)
//			return null;
//		
//		param.put("limit", page.getShowCount());
//		param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
//		
//		List<GroupAccountLogOutDTO>  groupAccountLogList = groupAccountLogMapper.selectGroupAccountLogList(param);
//		
//		groupAccountListOutDTO.setGroupAccountLogOutDTO(groupAccountLogList);
//	    return groupAccountListOutDTO;
//	}

	
	
	@Override
	public GroupAccountLogMonth queryMonthAccountById(Long id) throws Exception {
		return groupAccountLogMonthMapper.queryMonthAccountById(id);
	}


	
	@Override
	public List<GroupAccountLogDayOutDTO> getGroupAccountLogMonthDayList(GroupAccountInDTO groupAccountInDTO, Page page) {
		
		Map<String,Object> param=new HashMap<String,Object>();
		
//		param.put("accountId",groupAccountInDTO.getGroupId());
		param.put("accountId",groupAccountInDTO.getAccountId());
		param.put("stringStatisDate", groupAccountInDTO.getStringStatisDate());
		
		
		List<GroupAccountLogDayOutDTO> result = new ArrayList<GroupAccountLogDayOutDTO>();

		int sumCount = groupAccountLogDayMapper.selectGroupAccountLogDayFromMonthListCount(param);
		
		page.setTotalResult(sumCount);
		if(sumCount<=0)
			return null;
		
		param.put("limit", page.getShowCount());
		param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
		
		List<Map<String,Object>>  groupAccountLogDayList = groupAccountLogDayMapper.selectGroupAccountLogDayFromMonthList(param);
		
		
		for (Map<String, Object> groupAccountLogDay : groupAccountLogDayList) {
			GroupAccountLogDayOutDTO groupAccountLogDayOutDTO = new GroupAccountLogDayOutDTO();
			groupAccountLogDayOutDTO.setId((Long)groupAccountLogDay.get("id"));
			groupAccountLogDayOutDTO.setStatisDate((Date)groupAccountLogDay.get("statis_date"));
			groupAccountLogDayOutDTO.setAmountSum(Integer.parseInt(groupAccountLogDay.get("amount_sum").toString()));
			groupAccountLogDayOutDTO.setChangeType((Integer)groupAccountLogDay.get("change_type"));
			groupAccountLogDayOutDTO.setSubjectType((Integer)groupAccountLogDay.get("subject_type"));
			
			result.add(groupAccountLogDayOutDTO);
		}
		
		return result;
	}

	
	
	@Override
	public List<GroupAccountLogMonthOutDTO> queryGroupAccountLogMonthList(GroupAccountInDTO groupAccountInDTO,
			Page page) {
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("accountId",groupAccountInDTO.getAccountId());
		param.put("stringStatisDate", groupAccountInDTO.getStringStatisDate());
		param.put("subjectType", groupAccountInDTO.getSubjectType());
		
		List<GroupAccountLogMonthOutDTO> result = new ArrayList<GroupAccountLogMonthOutDTO>();

		int sumCount = groupAccountLogMonthMapper.queryGroupAccountLogMonthListCount(param);
		page.setTotalResult(sumCount);
		if(sumCount<=0)
			return null;
		
		param.put("limit", page.getShowCount());
		param.put("start", page.getShowCount()*(page.getCurrentPage()-1));
		
		List<Map<String,Object>>  groupAccountLogDayList = groupAccountLogMonthMapper.queryGroupAccountLogMonthList(param);
		
		
		for (Map<String, Object> groupAccountLogDay : groupAccountLogDayList) {
			GroupAccountLogMonthOutDTO groupAccountLogMonthOutDTO = new GroupAccountLogMonthOutDTO();
			
			groupAccountLogMonthOutDTO.setId((Long)groupAccountLogDay.get("id"));
			groupAccountLogMonthOutDTO.setStatisDate((String)groupAccountLogDay.get("statis_date"));
			groupAccountLogMonthOutDTO.setAmountSum(Integer.parseInt(groupAccountLogDay.get("amount_sum").toString()));
			groupAccountLogMonthOutDTO.setChangeType((Integer)groupAccountLogDay.get("change_type"));
			groupAccountLogMonthOutDTO.setSubjectType((Integer)groupAccountLogDay.get("subject_type"));
			
			result.add(groupAccountLogMonthOutDTO);
		}
		
		return result;
	}

	

	

	@Override
	public GroupAccountOutDTO updateAmount(GroupAccountInDTO groupAccountInDTO) throws Exception {
		//更新余额
		GroupAccount groupAccount = new GroupAccount();
	    groupAccount = groupAccountService.getAmount(groupAccountInDTO);
		Integer a = groupAccount.getBalance();
		Integer b = groupAccountInDTO.getAddBalance();
		groupAccount.setGroupId(groupAccountInDTO.getGroupId());
		groupAccount.setBalance(a+b);
		
		Integer Count = groupAccountMapper.updateAmount(groupAccount);
		if (Count==null ) {
			throw new Exception("更新余额失败但不抛异常");
		}
		
		GroupAccountLog GroupAccountLog = new GroupAccountLog();
//		GroupAccountLog.setId(groupAccountInDTO.getGroupId());
		GroupAccountLog.setDescInfo(groupAccountInDTO.getDescInfo());
		GroupAccountLog.setGmtCreate(new Date());
		GroupAccountLog.setGmtModified(new Date());
		GroupAccountLog.setAccountId(groupAccountInDTO.getGroupId());
		GroupAccountLog.setChangeType(BizEnum.SaleAccountSubjectType.AddAmount.getType());
		GroupAccountLog.setSubjectType(BizEnum.SaleAccountChangeType.Add.getType());
		Integer Count2=groupAccountLogMapper.insertSelective(GroupAccountLog);
		if (Count2==null ) {
			throw new Exception("更新余额失败但不抛异常");
		}
		
		return null;
	}


	//更新余额同时插入流水
	public int updateAmount(Long groupId, BizEnum.SaleAccountSubjectType subjectType
			, Integer amount,Long relateTableId,Long activityId,String desc) throws Exception{
		int result =0;
        Integer balance = 0;
		Integer lockAmount = 0;

		if(amount == null || amount <= 0)
			return result;

		BizEnum.SaleAccountChangeType changeType = null;
		BizEnum.SaleAccountChangeType lockChangeType = null;

		switch (subjectType){
			case AddAmount:
				changeType = BizEnum.SaleAccountChangeType.Add;
				balance = amount;
				break;
			case RedPackageCreate:
			case CouponCreate:
			case ConsumeBackCashCreate:
			case SignBackCashCreate:
				lockChangeType = BizEnum.SaleAccountChangeType.Freeze;
				lockAmount = amount;
				break;
			case RedPackageUnfreeze:
			case CouponUnfreeze:
			case ConsumeBackCashUnfreeze:
			case SignBackCashUnfreeze:
				lockChangeType = BizEnum.SaleAccountChangeType.Unfreeze;
				lockAmount = -1 * amount;
				break;
			case RedPackageGet:
			case CouponGet:
			case ConsumeBackCashGet:
			case SignBackCashGet:
				changeType = BizEnum.SaleAccountChangeType.Subtract;
				lockChangeType = BizEnum.SaleAccountChangeType.Unfreeze;
				balance = -1 * amount;
				lockAmount = -1 *amount;
				break;
		}

		GroupAccount groupAccount = groupAccountMapper.getAmount(groupId);
		groupAccount.setLockAmount(groupAccount.getLockAmount()==null?0:groupAccount.getLockAmount());
		groupAccount.setBalance(groupAccount.getBalance()==null?0:groupAccount.getBalance());
		//冻结金额判断
		if((groupAccount.getLockAmount() + lockAmount) > (groupAccount.getBalance() + balance)) {
//			throw new ApplicationException("营销账户(groupId:" + groupId.toString() + ")余额不足！(冻结金额：" + AmountUtils.changeF2Y((long)amount) + ")");
			throw new ApplicationException("营销账户余额不足！");
		}
		//使用金额判断
		if((groupAccount.getBalance() + balance) < 0 ) {
			throw new ApplicationException("营销账户(groupId:" + groupId.toString() + ")余额不足！(使用金额：" + AmountUtils.changeF2Y((long)balance) + ")");
		}
		GroupAccount groupAccountUpdate = new GroupAccount();
		groupAccountUpdate.setId(groupAccount.getId());
		groupAccountUpdate.setGmtModified(new Date());
		groupAccountUpdate.setBalance(groupAccount.getBalance() + balance);
		groupAccountUpdate.setLockAmount(groupAccount.getLockAmount() + lockAmount);
		groupAccountMapper.updateByPrimaryKeySelective(groupAccountUpdate);

		//增加余额变化日志流水
		if(balance != 0) {
			GroupAccountLog groupAccountLog = new GroupAccountLog();
			groupAccountLog.setGmtModified(new Date());
			groupAccountLog.setGmtCreate(new Date());
			groupAccountLog.setAccountId(groupAccount.getId());
			groupAccountLog.setAmount(balance);
			groupAccountLog.setAfterAmount(groupAccount.getBalance() + balance);
			groupAccountLog.setChangeType(changeType.getType());
			groupAccountLog.setSubjectType(subjectType.getType());
			groupAccountLog.setRelateTableId(relateTableId);
			groupAccountLog.setActivityId(activityId);
			groupAccountLog.setDescInfo(desc);
			groupAccountLogMapper.insert(groupAccountLog);
		}
		//增加锁定金额变化日志流水
		if(lockAmount != 0) {
			GroupAccountLog groupAccountLog = new GroupAccountLog();
			groupAccountLog.setGmtModified(new Date());
			groupAccountLog.setGmtCreate(new Date());
			groupAccountLog.setAccountId(groupAccount.getId());
			groupAccountLog.setAmount(lockAmount);
			groupAccountLog.setAfterAmount(groupAccount.getLockAmount() + lockAmount);
			groupAccountLog.setChangeType(lockChangeType.getType());
			groupAccountLog.setSubjectType(subjectType.getType());
			groupAccountLog.setRelateTableId(relateTableId);
			groupAccountLog.setActivityId(activityId);
			groupAccountLog.setDescInfo(desc);
			groupAccountLogMapper.insert(groupAccountLog);
		}
		return result;
	}


}
